﻿using AnesSystem.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AnesSystem.Util.Date
{
    /// <summary>
    /// TimePicker.xaml 的交互逻辑
    /// </summary>
    public partial class TimePicker : BaseToolWindow
    {
        public TimePicker()
        {
            InitializeComponent();
            this.textBlockmm.Background = System.Windows.Media.Brushes.White;
            this.textBlockhh.Background = System.Windows.Media.Brushes.White;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="txt"></param>
        public TimePicker(string txt)
        {
            InitializeComponent();
            this.textBlockmm.Background = System.Windows.Media.Brushes.White;
            this.textBlockhh.Background = System.Windows.Media.Brushes.White;
            textBlockhh.Text = DateTime.Now.Hour.ToString();
            textBlockmm.Text = DateTime.Now.Minute.ToString();
            this.formerDateTimeStr = txt;
        }

        #region 全局变量

        /// <summary>
        /// 从 DateTimePicker 传入的日期时间字符串
        /// </summary>
        private string formerDateTimeStr = string.Empty;

        // private string selectDate = string.Empty;    

        #endregion   

        #region 事件

        /// <summary>
        /// TDateTimeView 窗体登录事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            textBlockhh.Text = "00";
            textBlockmm.Text = "00";
        }


        /// <summary>
        /// 关闭按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void iBtnCloseView_Click(object sender, RoutedEventArgs e)
        {
            OnDateTimeContent(this.formerDateTimeStr);
        }

        /// <summary>
        /// 确定按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            DateTime? dt = new DateTime?();

            if (calDate.SelectedDate == null)
            {
                dt = DateTime.Now.Date;
            }
            else
            {
                dt = calDate.SelectedDate;
            }

            DateTime dtCal = Convert.ToDateTime(dt);

            string timeStr = "00:00:00";
            timeStr = textBlockhh.Text + ":" + textBlockmm.Text;

            string dateStr;
            dateStr = dtCal.ToString("yyyy/MM/dd");

            string dateTimeStr;
            dateTimeStr = dateStr + " " + timeStr;

            string str1 = string.Empty; ;
            str1 = dateTimeStr;
            OnDateTimeContent(str1);
            this.Close();
        }

        /// <summary>
        /// 当前按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnNow_Click(object sender, RoutedEventArgs e)
        {
           
            if (btnNow.Content.ToString() == "零点")
            {
                textBlockhh.Text = "00";
                textBlockmm.Text = "00";
                btnNow.Content = "当前";
                btnNow.Background = System.Windows.Media.Brushes.LightBlue;
            }
            else
            {
                DateTime dt = DateTime.Now;
                textBlockhh.Text = dt.Hour.ToString().PadLeft(2, '0');
                textBlockmm.Text = dt.Minute.ToString().PadLeft(2, '0');
                btnNow.Content = "零点";
                btnNow.Background = System.Windows.Media.Brushes.LightGreen;
            }


        }


        /// <summary>
        /// 解除calendar点击后 影响其他按钮首次点击无效的问题
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void calDate_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            if (Mouse.Captured is CalendarItem)
            {
                Mouse.Capture(null);
            }
        }


        #endregion

        #region Action交互

        /// <summary>
        /// 时间确定后的传递事件
        /// </summary>
        public Action<string> DateTimeOK;

        /// <summary>
        /// 时间确定后传递的时间内容
        /// </summary>
        /// <param name="dateTimeStr"></param>
        protected void OnDateTimeContent(string dateTimeStr)
        {
            if (DateTimeOK != null)
                DateTimeOK(dateTimeStr);
        }

        #endregion

        private void Up_Click(object sender, RoutedEventArgs e)
        {
            TextBox tb = sender as TextBox;
            int temp;
            if (this.textBlockhh.Background == System.Windows.Media.Brushes.Gray)
            {
                string hour = textBlockhh.Text.ToString();
                temp = System.Int32.Parse(hour);
                temp++;
                if (temp > 23)
                {
                    temp = 0;
                }
                textBlockhh.Text = temp.ToString();
            }
            else if (this.textBlockmm.Background == System.Windows.Media.Brushes.Gray)
            {
                string minute = textBlockmm.Text.ToString();
                temp = System.Int32.Parse(minute);
                temp++;
                if (temp > 59)
                {
                    temp = 0;
                }
                textBlockmm.Text = temp.ToString();
            }
            //else if (this.textBlockss.Background == System.Windows.Media.Brushes.Gray)
            //{
            //    string ss = textBlockss.Text.ToString();
            //    temp = System.Int32.Parse(ss);
            //    temp++;
            //    if (temp>59)
            //    {
            //        temp = 0;
            //    }
            //    textBlockss.Text = temp.ToString();
            //}
        }

        private void Down_Click(object sender, RoutedEventArgs e)
        {
            TextBox tb = sender as TextBox;
            int temp;
            if (this.textBlockhh.Background == System.Windows.Media.Brushes.Gray)
            {
                string hour = textBlockhh.Text.ToString();
                temp = System.Int32.Parse(hour);
                temp--;
                if (temp < 0)
                {
                    temp = 23;
                }
                textBlockhh.Text = temp.ToString();
            }
            else if (this.textBlockmm.Background == System.Windows.Media.Brushes.Gray)
            {
                string minute = textBlockmm.Text.ToString();
                temp = System.Int32.Parse(minute);
                temp--;
                if (temp < 0)
                {
                    temp = 59;
                }
                textBlockmm.Text = temp.ToString();
            }
           
        }
        private void Textbox_SelectionChanged(object sender, RoutedEventArgs e)
        {
            TextBox tb = sender as TextBox;
            if (tb != null)
            {
                switch (tb.Name)
                {
                    case "textBlockhh":
                        tb.Background = System.Windows.Media.Brushes.Gray;
                        if(this.textBlockmm!=null) this.textBlockmm.Background = System.Windows.Media.Brushes.White;
                        break;
                    case "textBlockmm":
                        tb.Background = System.Windows.Media.Brushes.Gray;
                        if (this.textBlockhh != null) this.textBlockhh.Background = System.Windows.Media.Brushes.White;
                        break;
                }
            }
        }
    }
}
